package com.zhangzeqin.code.view;

import com.zhangzeqin.code.controller.TimeController;
import com.zhangzeqin.code.controller.UserController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class RegisterHandler implements ActionListener {
    public RegisterView registerView;
    public LoginView loginView;
    /**
     * 用户控制层
     */
    UserController userController=new UserController();

    public RegisterHandler(){

    }
    public RegisterHandler(RegisterView registerView){
      this.registerView=registerView;
    }
    public RegisterHandler(LoginView loginView){
        this.loginView=loginView;
    }


    @Override
    public void actionPerformed(ActionEvent e) {

        JButton jButton=(JButton)e.getSource();
        String text = jButton.getText();


        if("确定注册".equals(text)) {
            String userName = registerView.getUserTxt().getText();
            String password = registerView.getPasswordField().getText();

            boolean check = check(userName, password);
            boolean register = false;
            if (check) {
                register = register(userName, password);
                if (register) {
                    JOptionPane.showMessageDialog(registerView, "注册成功！");
                }
                new LoginView();

            }
        }
    }
    public boolean register(String userName,String password){

        check(userName, password);
        Boolean register = userController.register(userName, password);
        return register;
    }

    private boolean check(String userName, String password) {
        if(userName ==null|| userName.length()<=0){
            JOptionPane.showMessageDialog(registerView, "用户名不能为空！");
            return false;
        }else
        {
            if(password ==null|| password.length()<=0){
                JOptionPane.showMessageDialog(registerView, "密码不能为空！");
                return false;
            }

        }
        return true;

    }
}
